<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  news
 */

class m_news_actions_IphoneFoldersAction extends f_core_Action
{
    public function execute()
    {        
        $lang = $this->getRequestParameter('lang', f_core_Context::getInstance()->getDefaultLang());
        $displayLang = $this->getRequestParameter('displayLang', $this->getDisplayLang());

        $this->setDisplayLang($displayLang);

        $rootNode = m_backoffice_services_RootNodeService::getInstance()->getModuleRootNode('news', $lang);

        $relManager = f_relation_Manager::getInstance();

        $folders = $relManager->getChildsByDocType($rootNode->getUniqueId(), 'modules_backoffice/folder', $lang);

        $iphone = m_iphone_services_IphoneService::getInstance();
        $xml = $iphone->getConstructorIphone();
        
            $xml->startElement('dict');
                $xml->writeElement('key', 'rows');

                if(count($folders) > 0)
                {
                    $xml->startElement('array');
                    foreach($folders as $folder)
                    {
                        $xml->startElement('dict');
                        $xml = $this->getRecursiveFolders($xml, $folder);
                        $xml->endElement();
                    }
                    
                    $xml->endElement();
                }

            $xml->endElement();
        $xml->endElement();
        $xml->endDocument();

        return $xml->outputMemory();
    }

    /**
     *
     * @param XMLWriter $xml
     * @param m_backoffice_documents_Folder $folder
     */
    private function getRecursiveFolders($xml, $folder)
    {
        $xml->writeElement('key', 'title');
        $xml->writeElement('string', $folder->getLabel());
        $xml->writeElement('key', 'folderID');
        $xml->writeElement('string', $folder->getUniqueId());

        $relManager = f_relation_Manager::getInstance();

        $folders = $relManager->getChildsByDocType($folder->getUniqueId(), 'modules_backoffice/folder', $lang);
        $news = $this->getDocumentProvider()
                    ->loadDocuments("modules_news/item")
                    ->isChildOf($folder->getUniqueId(), "item")
                    ->retrieveDocuments();

        if(count($folders) > 0 || count($news) > 0)
        {
            $xml->writeElement('key', 'children');
            $xml->startElement('array');

            if(count($folders) > 0)
            {
                foreach($folders as $folder)
                {
                    $xml->startElement('dict');
                    $xml = $this->getRecursiveFolders($xml, $folder);
                    $xml->endElement();
                }
            }

            if(count($news) > 0)
            {
                foreach($news as $new)
                {
                    $xml->startElement('dict');
                        $xml->writeElement('key', 'nid');
                        $xml->writeElement('integer', $new->getUniqueId());
                        $xml->writeElement('key', 'title');
                        $xml->writeElement('string', $new->getLabel());
                        $xml->writeElement('key', 'fid');
                        $xml->writeElement('integer', $folder->getUniqueId());
                        $xml->writeElement('key', 'content');
                        $xml->writeElement('string',  $new->getContent());
                        $xml->writeElement('key', 'mid');
                        $xml->writeElement('integer', $new->getPictures());

                    $xml->endElement();
                }
            }

            $xml->endElement();
        }

        return $xml;
    }
}